package com.tenton.servlet;

import com.tenton.service.Impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Date: 2021/1/13
 * @Author: Tenton
 * @Description: 删除用户
 */
@WebServlet(name = "DeleteUserServlet",urlPatterns = "/deleteUserServlet")
public class DeleteUserServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置编码格式，防止乱码
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        UserServiceImpl userServiceImpl = new UserServiceImpl();
        int id = Integer.parseInt(request.getParameter("id"));
        int result = userServiceImpl.deleteUser(id);
        if (result == 1){
            request.getRequestDispatcher("/listUserServlet").forward(request,response);
        }else {
            response.getWriter().write("删除失败，2秒后自动跳转到主界面");
            response.setHeader("Refresh","3;URL=/listUserServlet");
        }
    }
}
